/*
 * Copyright (C) 2016 mendhak
 *
 * This file is part of GPSLogger for Android.
 *
 * GPSLogger for Android is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * GPSLogger for Android is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GPSLogger for Android.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.blank.gps;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Strings {
//    /**
//     * Converts seconds into friendly, understandable description of the duration.
//     *
//     * @param numberOfSeconds
//     * @return
//     */
//    public static String getDescriptiveDurationString(int numberOfSeconds,
//                                                      Context context) {
//
//        String descriptive;
//        int hours;
//        int minutes;
//        int seconds;
//
//        int remainingSeconds;
//
//        // Special cases
//        if(numberOfSeconds==0){
//            return "";
//        }
//
//        if (numberOfSeconds == 1) {
//            return context.getString(R.string.time_onesecond);
//        }
//
//        if (numberOfSeconds == 30) {
//            return context.getString(R.string.time_halfminute);
//        }
//
//        if (numberOfSeconds == 60) {
//            return context.getString(R.string.time_oneminute);
//        }
//
//        if (numberOfSeconds == 900) {
//            return context.getString(R.string.time_quarterhour);
//        }
//
//        if (numberOfSeconds == 1800) {
//            return context.getString(R.string.time_halfhour);
//        }
//
//        if (numberOfSeconds == 3600) {
//            return context.getString(R.string.time_onehour);
//        }
//
//        if (numberOfSeconds == 4800) {
//            return context.getString(R.string.time_oneandhalfhours);
//        }
//
//        if (numberOfSeconds == 9000) {
//            return context.getString(R.string.time_twoandhalfhours);
//        }
//
//        // For all other cases, calculate
//
//        hours = numberOfSeconds / 3600;
//        remainingSeconds = numberOfSeconds % 3600;
//        minutes = remainingSeconds / 60;
//        seconds = remainingSeconds % 60;
//
//        // Every 5 hours and 2 minutes
//        // XYZ-5*2*20*
//
//        descriptive = String.format(context.getString(R.string.time_hms_format),
//                String.valueOf(hours), String.valueOf(minutes), String.valueOf(seconds));
//
//        return descriptive;
//
//    }
//
//    /**
//     * Converts given bearing degrees into a rough cardinal direction that's
//     * more understandable to humans.
//     *
//     * @param bearingDegrees
//     * @return
//     */
//    public static String getBearingDescription(float bearingDegrees,
//                                               Context context) {
//
//        String direction;
//        String cardinal;
//
//        if (bearingDegrees > 348.75 || bearingDegrees <= 11.25) {
//            cardinal = context.getString(R.string.direction_north);
//        } else if (bearingDegrees > 11.25 && bearingDegrees <= 33.75) {
//            cardinal = context.getString(R.string.direction_northnortheast);
//        } else if (bearingDegrees > 33.75 && bearingDegrees <= 56.25) {
//            cardinal = context.getString(R.string.direction_northeast);
//        } else if (bearingDegrees > 56.25 && bearingDegrees <= 78.75) {
//            cardinal = context.getString(R.string.direction_eastnortheast);
//        } else if (bearingDegrees > 78.75 && bearingDegrees <= 101.25) {
//            cardinal = context.getString(R.string.direction_east);
//        } else if (bearingDegrees > 101.25 && bearingDegrees <= 123.75) {
//            cardinal = context.getString(R.string.direction_eastsoutheast);
//        } else if (bearingDegrees > 123.75 && bearingDegrees <= 146.26) {
//            cardinal = context.getString(R.string.direction_southeast);
//        } else if (bearingDegrees > 146.25 && bearingDegrees <= 168.75) {
//            cardinal = context.getString(R.string.direction_southsoutheast);
//        } else if (bearingDegrees > 168.75 && bearingDegrees <= 191.25) {
//            cardinal = context.getString(R.string.direction_south);
//        } else if (bearingDegrees > 191.25 && bearingDegrees <= 213.75) {
//            cardinal = context.getString(R.string.direction_southsouthwest);
//        } else if (bearingDegrees > 213.75 && bearingDegrees <= 236.25) {
//            cardinal = context.getString(R.string.direction_southwest);
//        } else if (bearingDegrees > 236.25 && bearingDegrees <= 258.75) {
//            cardinal = context.getString(R.string.direction_westsouthwest);
//        } else if (bearingDegrees > 258.75 && bearingDegrees <= 281.25) {
//            cardinal = context.getString(R.string.direction_west);
//        } else if (bearingDegrees > 281.25 && bearingDegrees <= 303.75) {
//            cardinal = context.getString(R.string.direction_westnorthwest);
//        } else if (bearingDegrees > 303.75 && bearingDegrees <= 326.25) {
//            cardinal = context.getString(R.string.direction_northwest);
//        } else if (bearingDegrees > 326.25 && bearingDegrees <= 348.75) {
//            cardinal = context.getString(R.string.direction_northnorthwest);
//        } else {
//            direction = context.getString(R.string.unknown_direction);
//            return direction;
//        }
//
//        direction = context.getString(R.string.direction_roughly, cardinal);
//        return direction;
//
//    }

    /**
     * Makes string safe for writing to XML file. Removes lt and gt. Best used
     * when writing to file.
     *
     * @param desc
     * @return
     */
    public static String cleanDescriptionForXml(String desc) {
        desc = desc.replace("<", "");
        desc = desc.replace(">", "");
        desc = desc.replace("&", "&amp;");
        desc = desc.replace("\"", "&quot;");

        return desc;
    }

    public static String cleanDescriptionForJson(String desc){
        desc = desc.replace("\"", "");
        desc = desc.replace("\\","");
        return desc;
    }

    public static String getIsoDateTimeWithOffset(Date dateToFormat){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        SimpleDateFormat ISO_8601_DATE_TIME = new SimpleDateFormat(getIsoDateTimeWithOffsetFormat(), Locale.US);
        ISO_8601_DATE_TIME.setTimeZone(calendar.getTimeZone());
        String dateTimeStringWithOffset = ISO_8601_DATE_TIME.format(dateToFormat);
        //Fix for older Android where ZZZZZ does not include colon
        if (!dateTimeStringWithOffset.substring(dateTimeStringWithOffset.length() - 3).startsWith(":")) {
            dateTimeStringWithOffset = dateTimeStringWithOffset.substring(0, dateTimeStringWithOffset.length() - 2) + ":" + dateTimeStringWithOffset.substring(dateTimeStringWithOffset.length() - 2);
        }
        return dateTimeStringWithOffset;
    }

    /**
     * Given a Date object, returns an ISO 8601 date time string in UTC.
     * Example: 2010-03-23T05:17:22Z but not 2010-03-23T05:17:22+04:00
     *
     * @param dateToFormat The Date object to format.
     * @return The ISO 8601 formatted string.
     */
    public static String getIsoDateTime(Date dateToFormat) {
        /**
         * This function is used in gpslogger.loggers.* and for most of them the
         * default locale should be fine, but in the case of HttpUrlLogger we
         * want machine-readable output, thus  Locale.US.
         *
         * Be wary of the default locale
         * http://developer.android.com/reference/java/util/Locale.html#default_locale
         */

        // GPX specs say that time given should be in UTC, no local time.
        SimpleDateFormat sdf = new SimpleDateFormat(getIsoDateTimeFormat(),
                Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        return sdf.format(dateToFormat);
    }

    public static String getIsoDateTimeFormat() {
        return "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    }

    public static String getIsoDateTimeWithOffsetFormat() {
        return "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ";
    }


    /**
     * Given a Date object, returns an ISO 8601 calendar date string.
     * Example: 2010-03-23
     *
     * @param dateToFormat The Date object to format.
     * @return The ISO 8601 formatted string.
     */
    public static String getIsoCalendarDate(Date dateToFormat) {
        /**
         * This function is used in CustomUrlLogger.
         */

        // GPX specs say that time given should be in UTC, no local time.
        SimpleDateFormat sdf = new SimpleDateFormat(getIsoCalendarDateFormat(),
                Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        return sdf.format(dateToFormat);
    }

    public static String getIsoCalendarDateFormat() {
        return "yyyy-MM-dd";
    }

    public static String getReadableDateTime(Date dateToFormat) {
        /**
         * Similar to getIsoDateTime(), this function is used in
         * AutoEmailManager, and we want machine-readable output.
         */
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm",
                Locale.US);
        return sdf.format(dateToFormat);
    }

    /**
     * Checks if a string is null or empty
     *
     * @param text
     * @return
     */
    public static boolean isNullOrEmpty(String text) {
        return text == null ||  text.trim().length() == 0;
    }

    public static String htmlDecode(String text) {
        if (isNullOrEmpty(text)) {
            return text;
        }

        return text.replace("&amp;", "&").replace("&quot;", "\"");
    }




}
